package com.example.final_project.Comm.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.final_project.Comm.Activity.ContentActivity;
import com.example.final_project.Comm.entry.CommContent;
import com.example.final_project.R;

import java.util.List;

public class ContentAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<CommContent> commContent;
    private ImageView head,img1,img2,img3;
    private TextView name,content;

    public ContentAdapter(Context context, List<CommContent> commContent){
        this.context = context;
        this.commContent = commContent;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.comm_content_kid,parent,false);
        contentHolder contentHolder = new contentHolder(view);
        return contentHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        contentHolder contentHolder = (contentHolder)holder;
        contentHolder.setData(position);
    }

    @Override
    public int getItemCount() {
        return commContent.size();
    }

    class contentHolder extends RecyclerView.ViewHolder {
        public contentHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(int position){
            CommContent contents = commContent.get(position);
            head = itemView.findViewById(R.id.comm_head);
            head.setImageResource(R.mipmap.user_head);
            img1 = itemView.findViewById(R.id.comm_img1);
            if(contents.getImg1() == null){
                img1.setVisibility(View.GONE);
            }
            else{
                Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+contents.getImg1()).into(img1);
            }
            img2 = itemView.findViewById(R.id.comm_img2);
            if(contents.getImg2() == null){
                img2.setVisibility(View.GONE);
            }
            else{
                Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+contents.getImg2()).into(img2);
            }
            img3 = itemView.findViewById(R.id.comm_img3);
            if(contents.getImg2() == null){
                img3.setVisibility(View.GONE);
            }
            else{
                Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+contents.getImg3()).into(img3);
            }
            name = itemView.findViewById(R.id.comm_name);
            name.setText(contents.getName());
            content = itemView.findViewById(R.id.comm_content);
            content.setText(contents.getContent());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ContentActivity.class);
                    intent.putExtra("name",contents.getName());
                    context.startActivity(intent);
                }
            });
        }

    }

}
